package com.huitone.gddw.service.impl.worktable;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.SvrRouteRule;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISvrRouteRuleDao;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.ISvrRouteRuleService;
import com.huitone.gddw.service.dto.GateSvrLevelDTO;
import com.huitone.gddw.service.dto.SvrGateLevelDTO;
import com.huitone.gddw.service.dto.SvrRouteRuleDTO;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 短信业务路由规则 接口实现类
 * @author wuluodan
 *
 */
@Service
public class SvrRouteRuleService implements ISvrRouteRuleService {

	@Autowired
	private ISvrRouteRuleDao svrRouteRuleDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<SvrRouteRuleDTO> selectPage(QueryPage<SvrRouteRule> page) {
		Map<String, Object> map = page.getParams();
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SvrRouteRuleDTO> result = svrRouteRuleDao.selectPage(map);
		int totalCount = svrRouteRuleDao.selectCount(map);
		
		QueryPage<SvrRouteRuleDTO> queryPage = new QueryPage<SvrRouteRuleDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public SvrRouteRule selectByPrimaryKey(String ruleId) {
		return svrRouteRuleDao.selectByPrimaryKey(ruleId);
	}

	@Override
	public SvrRouteRuleDTO toUpdate(String ruleId) {
		return new SvrRouteRuleDTO(this.selectByPrimaryKey(ruleId));
	}

	@Override
	public void changeStatus(SvrRouteRule entity) {
		this.update(entity);
	}

	@Override
	public void save(SvrRouteRule entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		svrRouteRuleDao.insertSelective(entity);
		
		this.updateParamRec("保存短信业务路由规则");
	}

	@Override
	public void update(SvrRouteRule entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		svrRouteRuleDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新短信业务路由规则");
	}

	@Override
	public void delete(String ruleId) {
		svrRouteRuleDao.deleteByPrimaryKey(ruleId);
		this.updateParamRec("删除短信业务路由规则");
	}

	@Override
	public List<SvrRouteRuleDTO> getRecord(Map<String, Object> params) {
		List<SvrRouteRuleDTO> list = svrRouteRuleDao.getRecord(params);
		return list;
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SVR_ROUTE_RULE, desc);
	}

	@Override
	public List<SvrRouteRule> getByGateLinkId(String gateLinkId) {
		return svrRouteRuleDao.getByGateLinkId(gateLinkId);
	}

	@Override
	public List<String> getGateIdByService(String serviceCode) {
		return svrRouteRuleDao.getGateIdByService(serviceCode);
	}

	@Override
	public List<SvrRouteRule> getByServiceCode(String serviceCode,String gateLinkId) {
		return svrRouteRuleDao.getServiceCode(serviceCode,gateLinkId);
	}

	@Override
	public QueryPage<SvrGateLevelDTO> selectPageByServiceCode(QueryPage<SvrRouteRule> page) {
		Map<String, Object> map = page.getParams();
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SvrGateLevelDTO> result = new ArrayList<>();
		int totalCount = svrRouteRuleDao.selectCountByServiceCode(map);
		List<String> serviceCodeList= svrRouteRuleDao.selectServiceCode(map);
		
		for(String serviceCode : serviceCodeList){
			SvrGateLevelDTO svrGateLevelDTO = this.getRouteGate(serviceCode);
			result.add(svrGateLevelDTO);
		}
		
		QueryPage<SvrGateLevelDTO> queryPage = new QueryPage<SvrGateLevelDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public Message saveRouteGate(SvrGateLevelDTO record, String type) {
		List<SvrRouteRuleDTO> gateList = svrRouteRuleDao.selectGateByServiceCode(record.getServiceCode(),null);
		if (type.equals("1") && (!gateList.isEmpty() || gateList.size()>0)) {
			return new Message(false, "该短信类型的路由通道信息已存在！");
		}
		if ((null==record.getFirstGateIds() || record.getFirstGateIds()=="") && (null==record.getSecondGateIds() || record.getSecondGateIds()=="")) {
			return new Message(false, "请选择通道！");
		}
		List<SvrRouteRule> list = new ArrayList<>();
		String[] firstGateIds = record.getFirstGateIds().split(",");
		String[] secondGateIds = record.getSecondGateIds().split(",");
		for(String fId : firstGateIds){
			if ((record.getSecondGateIds().contains(fId)) && !StringUtils.isEmpty(fId)) {
				return new Message(false, "第一优先级和第二优先级的通道有重复！");
			}
			SvrRouteRule entity = new SvrRouteRule();
			entity.setRuleId(StringUtils.uuid());
			entity.setServiceCode(record.getServiceCode());
			entity.setGateLinkId(fId);
			entity.setStatus("1");
			entity.setRouteLevel("1");
			entity.setInsertTime(new Date());
			entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
			entity.setLastModTime(entity.getInsertTime());
			entity.setLastModStaffId(entity.getInsertStaffId());
			list.add(entity);
		}
		for(String sId : secondGateIds){
			SvrRouteRule entity = new SvrRouteRule();
			entity.setRuleId(StringUtils.uuid());
			entity.setServiceCode(record.getServiceCode());
			entity.setGateLinkId(sId);
			entity.setStatus("1");
			entity.setRouteLevel("2");
			entity.setInsertTime(new Date());
			entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
			entity.setLastModTime(entity.getInsertTime());
			entity.setLastModStaffId(entity.getInsertStaffId());
			list.add(entity);
		}
		if (type.equals("2")) {	//修改，先删除之前的信息再保存
			svrRouteRuleDao.deleteByServiceCode(record.getOldServiceCode());
		}
		if (!list.isEmpty() && list.size() > 0) {
			svrRouteRuleDao.insertBatch(list);
		}
		if (type.equals("2")) {
			this.updateParamRec("修改路由通道信息");
			return new Message(true, "修改成功！");
		}
		this.updateParamRec("保存路由通道信息");
		return new Message(true, "保存成功！");
	}
	
	@Override
	public SvrGateLevelDTO getRouteGate(String serviceCode){
		
		SvrGateLevelDTO svrGateLevelDTO = new SvrGateLevelDTO();
		svrGateLevelDTO.setServiceCode(serviceCode);
		//第一优先级通道
		List<SvrRouteRuleDTO> list1 = svrRouteRuleDao.selectGateByServiceCode(serviceCode, "1");
		if(list1.size()>0){
			svrGateLevelDTO.setServiceName(list1.get(0).getServiceName());
			String firstGateNames = "";
			String firstGateIds = "";
			for(SvrRouteRuleDTO svrRouteRuleDTO : list1){
				firstGateNames += svrRouteRuleDTO.getGateLinkName() + ",";
				firstGateIds += svrRouteRuleDTO.getGateLinkId() + ",";
			}
			firstGateNames = firstGateNames.substring(0, firstGateNames.length()-1);
			firstGateIds = firstGateIds.substring(0, firstGateIds.length()-1);
			svrGateLevelDTO.setFirstGateNames(firstGateNames);
			svrGateLevelDTO.setFirstGateIds(firstGateIds);
		}
		
		//第二优先级通道
		List<SvrRouteRuleDTO> list2 = svrRouteRuleDao.selectGateByServiceCode(serviceCode, "2");
		if (list2.size()>0) {
			svrGateLevelDTO.setServiceName(list2.get(0).getServiceName());
			String secondGateNames = "";
			String secondGateIds = "";
			for(SvrRouteRuleDTO svrRouteRuleDTO : list2){
				secondGateNames += svrRouteRuleDTO.getGateLinkName() + ",";
				secondGateIds += svrRouteRuleDTO.getGateLinkId() + ",";
			}
			secondGateNames = secondGateNames.substring(0, secondGateNames.length()-1);
			secondGateIds = secondGateIds.substring(0, secondGateIds.length()-1);
			svrGateLevelDTO.setSecondGateNames(secondGateNames);
			svrGateLevelDTO.setSecondGateIds(secondGateIds);
		}
		return svrGateLevelDTO;
	}

	@Override
	public void deleteByServiceCode(String serviceCode) {
		svrRouteRuleDao.deleteByServiceCode(serviceCode);
		this.updateParamRec("删除路由通道信息");
	}

	@Override
	public QueryPage<GateSvrLevelDTO> selectPageByGateLinkId(QueryPage<SvrRouteRule> page) {
		Map<String, Object> map = page.getParams();
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<GateSvrLevelDTO> result = new ArrayList<>();
		int totalCount = svrRouteRuleDao.selectCountByGateLinkId(map);
		List<String> gateList= svrRouteRuleDao.getGateLinkId(map);
		
		for(String gateLinkId : gateList){
			GateSvrLevelDTO gateSvrLevelDTO = this.getRouteGateByGateLinkId(gateLinkId);
			result.add(gateSvrLevelDTO);
		}
		
		QueryPage<GateSvrLevelDTO> queryPage = new QueryPage<GateSvrLevelDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public GateSvrLevelDTO getRouteGateByGateLinkId(String gateLinkId) {
		GateSvrLevelDTO gateSvrLevelDTO = new GateSvrLevelDTO();
		gateSvrLevelDTO.setGateLinkId(gateLinkId);
		//第一优先级对应业务端
		List<SvrRouteRuleDTO> list1 = svrRouteRuleDao.selectServiceCodeByGate(gateLinkId, "1");
		if(list1.size()>0){
			gateSvrLevelDTO.setGateLinkName(list1.get(0).getGateLinkName());
			String firstSvrNames = "";
			String firstSvrIds = "";
			for(SvrRouteRuleDTO svrRouteRuleDTO : list1){
				firstSvrNames += svrRouteRuleDTO.getServiceName() + ",";
				firstSvrIds += svrRouteRuleDTO.getServiceCode() + ",";
			}
			firstSvrNames = firstSvrNames.substring(0, firstSvrNames.length()-1);
			firstSvrIds = firstSvrIds.substring(0, firstSvrIds.length()-1);
			gateSvrLevelDTO.setFirstSvrNames(firstSvrNames);
			gateSvrLevelDTO.setFirstSvrIds(firstSvrIds);
		}
		
		//第二优先级通道
		List<SvrRouteRuleDTO> list2 = svrRouteRuleDao.selectServiceCodeByGate(gateLinkId, "2");
		if (list2.size()>0) {
			gateSvrLevelDTO.setGateLinkName(list2.get(0).getGateLinkName());
			String secondSvrNames = "";
			String secondSvrIds = "";
			for(SvrRouteRuleDTO svrRouteRuleDTO : list2){
				secondSvrNames += svrRouteRuleDTO.getServiceName() + ",";
				secondSvrIds += svrRouteRuleDTO.getServiceCode() + ",";
			}
			secondSvrNames = secondSvrNames.substring(0, secondSvrNames.length()-1);
			secondSvrIds = secondSvrIds.substring(0, secondSvrIds.length()-1);
			gateSvrLevelDTO.setSecondSvrIds(secondSvrIds);
			gateSvrLevelDTO.setSecondSvrNames(secondSvrNames);
		}
		return gateSvrLevelDTO;
	}

	@Override
	public void deleteByGateLinkId(String gateLinkId) {
		svrRouteRuleDao.deleteByGate(gateLinkId);
		this.updateParamRec("删除路由通道信息");
	}

	@Override
	public Message saveRouteGate2(GateSvrLevelDTO record, String type) {
		List<SvrRouteRuleDTO> gateList = svrRouteRuleDao.selectServiceCodeByGate(record.getGateLinkId(),null);
		if (type.equals("1") && (!gateList.isEmpty() || gateList.size()>0)) {
			return new Message(false, "该通道的路由通道信息已存在！");
		}
		if ((null==record.getFirstSvrIds() || record.getFirstSvrIds()=="") && (null==record.getSecondSvrIds() || record.getSecondSvrIds()=="")) {
			return new Message(false, "请选择短信类型！");
		}
		List<SvrRouteRule> list = new ArrayList<>();
		String[] firstSvrNodes = record.getFirstSvrIds().split(",");
		String[] secondSvrNodes = record.getSecondSvrIds().split(",");
		for(String fId : firstSvrNodes){
			if (record.getSecondSvrIds().contains(fId) && !StringUtils.isEmpty(fId)) {
				return new Message(false, "第一优先级和第二优先级的短信类型有重复！");
			}
			SvrRouteRule entity = new SvrRouteRule();
			entity.setRuleId(StringUtils.uuid());
			entity.setServiceCode(fId);
			entity.setGateLinkId(record.getGateLinkId());
			entity.setStatus("1");
			entity.setRouteLevel("1");
			entity.setInsertTime(new Date());
			entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
			entity.setLastModTime(entity.getInsertTime());
			entity.setLastModStaffId(entity.getInsertStaffId());
			list.add(entity);
		}
		for(String sId : secondSvrNodes){
			SvrRouteRule entity = new SvrRouteRule();
			entity.setRuleId(StringUtils.uuid());
			entity.setServiceCode(sId);
			entity.setGateLinkId(record.getGateLinkId());
			entity.setStatus("1");
			entity.setRouteLevel("2");
			entity.setInsertTime(new Date());
			entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
			entity.setLastModTime(entity.getInsertTime());
			entity.setLastModStaffId(entity.getInsertStaffId());
			list.add(entity);
		}
		if (type.equals("2")) {	//修改，先删除之前的信息再保存
			svrRouteRuleDao.deleteByGate(record.getOldGateLinkId());
		}
		if (!list.isEmpty() && list.size() > 0) {
			svrRouteRuleDao.insertBatch(list);
		}
		if (type.equals("2")) {
			this.updateParamRec("修改路由通道信息");
			return new Message(true, "修改成功！");
		}
		this.updateParamRec("保存路由通道信息");
		return new Message(true, "保存成功！");
	}

}
